function [h,h_legend] = clusterView(cluster,gtruth,cameras,camera_label,titolo,allClust,putLegend)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Copyright (c) 2017 Image Processing Research Group of University Federico II of Naples ('GRIP-UNINA').
% All rights reserved.
% this software should be used, reproduced and modified only for informational and nonprofit purposes.
% 
% By downloading and/or using any of these files, you implicitly agree to all the
% terms of the license, as specified in the document LICENSE.txt
% (included in this package) and online at
% http://www.grip.unina.it/download/LICENSE_OPEN.txt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

h = figure();
cam = unique(camera_label,'stable');

% [hi,cam] = hist(camera_label,unique(camera_label));
% [~,ihi] = sort(hi,'descend');
% cam = cam(ihi);
% cameras = cameras(ihi);

CT =  crosstab(cluster,gtruth);

[~,iCT] = sort(sum(CT,2),'descend');
CT = CT(iCT,:);
 
if (allClust)
    b = bar(CT,'stacked');
else
     bar(CT(sum(CT,2)>1,:),'stacked');
end
h_legend = [];

if putLegend
    for i = 1:numel(cameras)
        leg{i}= [cameras{find(cam==cam(i))},' (',num2str( sum(camera_label==cam(i))),')'];
    end
    h_legend = legend(strrep(leg,'\',' '), 'Interpreter', 'none','Location','SouthEast');
end
title(titolo,'FontSize',12);
xlabel('cluster');
ylabel('# images');
set(gca,'XTick',[1:size(CT,1)]);

if (allClust)
xlim([0 size(CT,1)+1]);
else
xlim([0 sum(sum(CT,2)>1)+1]);    
end
        set(h_legend,'location','NorthEast');
end